function dist = catchSSB_dist(est0,H0,SSB0,normH,normSSB,R,T,w_a,m_a,mature)
% input: 
% est0 = initial guess of parameter values
% H0 = observed catch data (standerizded time series, kg)
% SSB0 = observed SSB (standerizded time series, kg)
% R = observed recruitment data (time series, number of fish)
% T = number of age classes
% w_a = weight per fish (kg)
% mature = dummy variable indicating age at maturity
% m_a = natural mortality

age = 1:T;
index_ssb = (SSB0>=0); 
index2_R = (R<=0);
ttmax = length(H0); % time horizon

R0 = max(R)*est0(1); % Exog recruitment before 1968
lambda0 = est0(2); % distribution parameter for starting year (1950)
N0 = R0*exp(-lambda0*age');

f0 = est0(3:end); % fishing mortality (1950-2015)

z = zeros(T,ttmax); % total mortality (a,t)
for t = 1:ttmax
    z(:,t) = f0(t)+ m_a;
end

% population matrix simulation ---------%
N = zeros(T+1,ttmax); % row=age; column = year
N(1,:) = R'; 
N(1,index2_R)=R0; % exog recruitment (before 1968)
N(2:end,1) = N0;
for t = 2:ttmax
    for j = 2:(T+1)
        N(j,t) = N(j-1,t-1)*exp(-z(j-1,t-1)); 
    end % for j
end % for t

% predicted catch & SSB ----------------------%
H_hat = zeros(ttmax,1);
SSB_hat = zeros(ttmax,1); % predicted spawning stock biomass
SSB_hat(1) = w_a.*(mature')*N(2:end,1);
for t = 1:ttmax
    H_hat(t)= (f0(t)./z(:,t).*(1-exp(-z(:,t))))'.*w_a*N(2:end,t);
    SSB_hat(t) = w_a.*(mature')*N(2:end,t);   
end % for t

v1 = H0 -(H_hat./normH); 
v2 = SSB0(index_ssb)-(SSB_hat(index_ssb)./normSSB);
dist = norm(v1)+norm(v2); % norm-2 distance

end